package com.boss.salary.dao.impl;

import com.boss.common.util.DateUtils;
import com.boss.framework.dao.BaseDaoImpl;
import com.boss.framework.dto.PaginationDTO;
import com.boss.salary.constant.SystemDictConstant;
import com.boss.salary.constant.TableName;
import com.boss.salary.dao.ISalWorkflowBackDao;
import com.boss.sys.utils.SysUtils;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Repository
public class SalWorkflowBackDaoImpl  extends BaseDaoImpl implements ISalWorkflowBackDao {
    @Override
    public List<Map<String, Object>> queryWorkflowBackPage(PaginationDTO paginationDTO,String province) {
        List<Object> paramList = new ArrayList<>();
        StringBuilder sql = new StringBuilder();
        sql.append(" select id, menu_id,menu_name, backto_menu_id,backto_menu_name ")
                .append(" from ").append(TableName.SAL_WORKFLOW_BACK)
                .append(" where is_deleted=? and mof_div_code=? ");
        paramList.add(SystemDictConstant.NOT_DELETED);
        paramList.add(province);
        sql.append(" order by id ");
        String querySql = this.boxUpSql(sql.toString());
        Object[] params = boxUpParam(paramList.toArray(), paginationDTO);
        return super.getjdbcTemplate().queryForList(querySql,params);
    }

    @Override
    public int queryWorkflowBackPageCount(String province) {
        List<Object> paramList = new ArrayList<>();
        StringBuilder sql = new StringBuilder();
        sql.append(" select count(*) ")
                .append(" from ").append(TableName.SAL_WORKFLOW_BACK);
//                .append(" where is_deleted=? and mof_div_code=? ");
//        paramList.add(SystemDictConstant.NOT_DELETED);
//        paramList.add(province);
        return this.getjdbcTemplate().queryForObject(sql.toString(), paramList.toArray(), Integer.class);
    }

    @Override
    public void insertWorkflowBack(Map<String, Object> params) {
        StringBuilder sql = new StringBuilder();
        sql.append("insert into ").append(TableName.SAL_WORKFLOW_BACK);
        sql.append("(id, menu_id,menu_name, backto_menu_id,backto_menu_name, mof_div_code, year, create_time, update_time, is_deleted) ")
                .append("values (?,?,?,?,?,?,?,?,?,?)");
        Object[] objects = new Object[]{
                SysUtils.generateGuid(),params.get("menu_id"),params.get("menu_name"),
                params.get("backto_menu_id"),params.get("backto_menu_name"),params.get("mof_div_code"),
                params.get("year"),String.valueOf(DateUtils.getCurrentTimeStamp()),
                String.valueOf(DateUtils.getCurrentTimeStamp()), SystemDictConstant.NOT_DELETED
        };
        this.getJdbcTemplate().update(sql.toString(), objects);
    }

    @Override
    public void updateWorkflowBack(Map<String, Object> params) {
        StringBuilder sb = new StringBuilder();
        sb.append(" update ").append(TableName.SAL_WORKFLOW_BACK)
                .append(" set update_time=?,menu_id=?,menu_name=?,backto_menu_id=?,backto_menu_name=? ")
                .append(" where ").append(" id=? ");
        Object[] objects=new Object[]{
                String.valueOf(DateUtils.getCurrentTimeStamp()),params.get("menu_id"),params.get("menu_name"),
                params.get("backto_menu_id"),params.get("backto_menu_name"),params.get("id")
        };
        super.getjdbcTemplate().update(sb.toString(),objects);
    }

    @Override
    public void deleteWorkflowBack(String id) {
        StringBuilder sb = new StringBuilder();
        sb.append(" update ").append(TableName.SAL_WORKFLOW_BACK)
                .append(" set is_deleted=? ")
                .append(" where ").append(" id=? ");
        Object[] objects=new Object[]{
                SystemDictConstant.IS_DELETED,id
        };
        super.getjdbcTemplate().update(sb.toString(),objects);
    }

    @Override
    public String getBackMenuId(String province, String nowMenuId) {
        StringBuilder sb = new StringBuilder();
        sb.append(" select backto_menu_id from ").append(TableName.SAL_WORKFLOW_BACK)
                .append(" where ").append(" mof_div_code = ? and menu_id=? and is_deleted=?");
        Object[] objects=new Object[]{
                province,nowMenuId, SystemDictConstant.NOT_DELETED
        };
        try {
            return super.getjdbcTemplate().queryForObject(sb.toString(), String.class, objects);
        }catch (Exception e) {
            return "";
        }
    }

    @Override
    public String getIsExist(String province, String nowMenuId,String id) {
        StringBuilder sb = new StringBuilder();
        sb.append(" select backto_menu_id from ").append(TableName.SAL_WORKFLOW_BACK)
                .append(" where ").append(" mof_div_code = ? and menu_id=? and is_deleted=? and id<>?");
        Object[] objects=new Object[]{
                province,nowMenuId, SystemDictConstant.NOT_DELETED,id
        };
        try {
            return super.getjdbcTemplate().queryForObject(sb.toString(), String.class, objects);
        }catch (Exception e) {
            return "";
        }
    }
}
